{include $data.pathToError}
<script type="text/javascript">
    {literal}
        
    $(document).ready(function() {
        $("#date").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1    
        });
        $(".float").change(function() {
            makeFloat(".float",1);
        });
        $(".double").change(function() {
            makeFloat(".double",2);
        })    
    });    
        
    function showReport(value){
        if (value!=-1){
            
            var data = {
                action : 'show_monthly_report_of_client',
                params: {
                    client: value
                }
            }

            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    result = false;
                } else {
                    $("#report").empty();
                    $("#report").append(data.text)
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    function changeTariffs(value){
        if (value!=-1){
            
            var data = {
                action : 'change_tariffs',
                params: {
                    type: value
                }
            }

            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#tariff").empty();
                    $("#tariff").append('<option value="-1">Please select</option>');
                    result = false;
                } else {
                    $("#tariff").empty();
                    $("#tariff").append('<option value="-1">Please select</option>');
                    $("#tariff").append(data.options)
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    function fillPrice(value){
        if (value!=-1){
            
            var data = {
                action : 'fill_price',
                params: {
                    tariff: value
                }
            }

            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#price").val("0.00");
                    result = false;
                } else {
                    $("#price").val(data.price);
                    makeFloat(".double",2);
                    result = true;
                }
            });

            return result;
            
        }
    }
    
    {/literal}
</script>
{if $data.newRecords}
    <h2 class="pageTitle">Records added during this session</h2>
    <table class="list">
        <tr>
            <th>Date</th>
            <th>Client</th>
            <th>Type of record</th>
            <th>Amount</th>
            <th>Tariff</th>
            <th>Unit price</th>
            <th>Description</th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
        {assign var=counter value=1}
        {foreach $data.newRecords as $key => $new}
            {if $counter==1}
                {assign var=bgClass value='bgDark'}
            {else}
                {assign var=bgClass value='bgLight'}
            {/if}
            <tr class="{$bgClass}">
                {if $counter==1}
                    {assign var=counter value=2}
                {else}
                    {assign var=counter value=1}
                {/if}
                <td>{$new.date_formated}</td>
                <td>{$new.client_info.name}</td>
                <td>{$new.type_info.type_of_record}</td>
                <td>{$new.amount}</td>
                <td>
                    {if $new.tariff==-1}
                        -
                    {else}
                        {$new.tariff_info.title}
                    {/if}
                </td>
                <td>{$new.unit_price}</td>
                <td>{$new.description}</td>
                <td><a class="baseLink" href="{$data.root}?input=edit_record&amp;id={$new.recordId}">Edit</a></td>
                <td><a class="baseLink" href="{$data.root}?input=add_record&amp;copy_id={$new.recordId}">Copy</a></td>
                <td><a class="baseLink" href="javascript: deleteRecord({$new.recordId},'add_record')">Delete</a></td>
            </tr>
        {/foreach}
    </table>
{/if}
<h2 class="pageTitle">Add timesheet-record</h2>
<form class="adding" action="{$data.root}?input=add_record" method="post" onsubmit="return checkAdd_record(
                                                                    $('#date').val(), 
                                                                    $('#client').val(),
                                                                    $('#type').val(),
                                                                    $('#amount').val(),
                                                                    $('#price').val(),
                                                                    $('#description').val()
                                                                                   )">
    <table class="tableForm">
        <tr>
            <td>
                <label for="date">Date <span class="red">*</span> </label>
            </td>
            <td>
                <input name="record[date]" id="date" type="text" value="{if $data.prefill.date}{$data.prefill.date}{else}{$smarty.now|date_format:"%d/%m/%Y"}{/if}" />
            </td>
            <td id="report" rowspan="7" style="width: 600px; padding-left: 20px">
                {if $data.prefill.report}{$data.prefill.report}{/if}
            </td>    
        </tr>
        <tr>
            <td>
                <label for="client">Client<span class="red">*</span> </label>
            </td>
            <td>
                <select name="record[client]" id="client" size="1" onchange="showReport(this.value)">
                    <option value="-1">Please select</option>
                    {foreach $data.clients as $key => $client}
                        <option {if $data.prefill.client && $data.prefill.client==$client.klient_id}selected="selected"{/if} value="{$client.klient_id}">{$client.name}</option>
                    {/foreach}
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <label for="type">Type of record<span class="red">*</span> </label>
            </td>
            <td>
                <select name="record[type]" id="type" size="1" onchange="changeTariffs(this.value)">
                    <option value="-1">Please select</option>
                    {foreach $data.types as $key => $type}
                        <option {if $data.prefill.type && $data.prefill.type==$type.Id}selected="selected"{elseif $type.Id==1}selected="selected"{/if} value="{$type.Id}">{$type.type_of_record}</option>
                    {/foreach}
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <label for="amount">Amount <span class="red">*</span> </label>
            </td>
            <td>
                <input name="record[amount]" id="amount" class="float" type="text" value="{if $data.prefill.amount}{$data.prefill.amount}{/if}" />
            </td>
        </tr>
        <tr>
            <td>
                <label for="tariff">Tariff<span class="red"></span> </label>
            </td>
            <td>
                <select name="record[tariff]" id="tariff" size="1" onchange="fillPrice(this.value)">
                    <option value="-1">Please select</option>
                    {foreach $data.tariffs as $key => $tariff}
                        <option {if $data.prefill.tariff && $data.prefill.tariff==$tariff.Id}selected="selected"{/if} value="{$tariff.Id}">{$tariff.title}</option>
                    {/foreach}
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <label for="price">Unit price<span class="red">*</span> </label>
            </td>
            <td>
                <input name="record[price]" id="price" class="double" type="text" value="{if $data.prefill.unit_price}{$data.prefill.unit_price}{/if}" />
            </td>
        </tr>
        <tr>
            <td>
                <label for="description">Description <span class="red">*</span></label>
            </td>
            <td>
                <textarea cols="30" rows="5" name="record[description]" id="description">{if $data.prefill.description}{$data.prefill.description}{/if}</textarea>
            </td>
        </tr>
        <tr class="submitRow">
            <td>
                <input name="action" value="add_record" type="hidden" />
            </td>
            <td>
                <input type="submit" value="Add record" name="add_record" /> &nbsp; 
                <span class="red">* Required</span>
            </td>
            <td></td>
        </tr>
    </table>
</form>